package com.avo.ramcache.config;

import com.avo.ramcache.manager.CacheServiceManager;
import com.avo.ramcache.orm.Accessor;
import com.avo.ramcache.orm.Querier;
import com.avo.ramcache.persist.PersisterConfig;
import org.springframework.beans.factory.FactoryBean;

import javax.annotation.PreDestroy;
import java.util.Map;
import java.util.Set;

public class CacheServiceManagerFactory implements FactoryBean<CacheServiceManager> {

    private Accessor accessor;
    private Querier querier;
    private Set<Class<IEntity<?>>> entityClasses;
    private Map<String, PersisterConfig> persisterConfigs;
    private Map<String, Integer> constants;
    private CacheServiceManager cacheServiceManager;

    @Override
    public CacheServiceManager getObject() {
        this.cacheServiceManager = new CacheServiceManager(entityClasses, accessor, querier, constants, persisterConfigs);
        return this.cacheServiceManager;
    }

    @Override
    public Class<?> getObjectType() {
        return CacheServiceManager.class;
    }

    @PreDestroy
    public void shutdown() {
        if (this.cacheServiceManager != null) {
            this.cacheServiceManager.shutdown();
        }
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setAccessor(Accessor accessor) {
        this.accessor = accessor;
    }

    public void setQuerier(Querier querier) {
        this.querier = querier;
    }

    public void setEntityClasses(Set<Class<IEntity<?>>> entityClasses) {
        this.entityClasses = entityClasses;
    }

    public void setPersisterConfigs(Map<String, PersisterConfig> persisterConfigs) {
        this.persisterConfigs = persisterConfigs;
    }

    public void setConstants(Map<String, Integer> constants) {
        this.constants = constants;
    }

    public void setCacheServiceManager(CacheServiceManager cacheServiceManager) {
        this.cacheServiceManager = cacheServiceManager;
    }
}
